package com.e3.web.widget.client;

import java.util.List;

import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;

public class MyFormPanel extends FormPanel {
	
	private int columnCount = 1;

	public int getColumnCount() {
		return columnCount;
	}

	public void setColumnCount(int columnCount) {
		this.columnCount = columnCount;
	}
	
	public void setFormFields(List<Field<?>> fields){
		if(fields != null){
			LayoutContainer main = new LayoutContainer();
			main.setLayout(new ColumnLayout());
			int size = getColumnCount();
			float colWidth = (float) (1.0/size);
			LayoutContainer[] lcs = new LayoutContainer[size];
			for (int i = 0; i < size; i++) {
				LayoutContainer lc = new LayoutContainer(new FormLayout());
				lcs[i] = lc;
				main.add(lc,new ColumnData(colWidth));
			}
			FormData formData = new FormData("100%");
			int j=0;
			for (int i = 0; i < fields.size(); i++) {
				MyField<?> field = (MyField<?>)fields.get(i);
				if(field.isHide()){
					add(fields.get(i));
					continue;
				}
				lcs[j].add(fields.get(i),formData);
				if(j==size-1){
					j=0;
				}
				else{
					j++;
				}
			}
			add(main,new FormData("100%"));
		}
	}
}
